package com.zw.admin.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.iot.model.v20180120.*;
import com.zw.admin.server.model.SysEquipment;
import com.zw.admin.server.service.ChanSerivce;
import com.zw.admin.server.service.EquipmentSerivce;
import com.zw.admin.server.utils.AbstractManager;
import com.zw.admin.server.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class EquipmentSerivceImpl implements EquipmentSerivce {


	private static DefaultAcsClient client = AbstractManager.getClient();

	@Autowired
	private ChanSerivce chanSerivce;


	@Override
	public Map saveEquipment(SysEquipment sysEquipment) {
		return null;
	}

	@Override
	public QueryProductResponse.Data queryEquipment(String productKey) {
		return null;
	}

	@Override
	public UpdateProductResponse updateEquipment(SysEquipment sysEquipment) {
		return null;
	}

	@Override
	public List<QueryDeviceResponse.DeviceInfo> QueryEquipmentList(Integer currentPage, Integer limit, Object o) {
		QueryProductListResponse.Data list = chanSerivce.QueryProductList(1, 200, null);
		List<QueryDeviceResponse.DeviceInfo> infoList = new ArrayList<>();
		for (int i = 0; i <list.getTotal() ; i++) {
			if (list.getList().get(i).getDeviceCount()>0) {
				QueryDeviceResponse response = null;
				QueryDeviceRequest request = new QueryDeviceRequest();
				request.setProductKey(list.getList().get(i).getProductKey());
				try {
					response = client.getAcsResponse(request);
					for (int j = 0; j < response.getTotal(); j++) {
						infoList.add(response.getData().get(j));
					}
					if (response.getSuccess() != null && response.getSuccess()) {
						LogUtil.print("产品下设备列表查询成功");
						LogUtil.print(JSON.toJSONString(response));
					} else {
						LogUtil.print("产品下设备列表查询失败");
						LogUtil.error(JSON.toJSONString(response));
					}
				} catch (ClientException e) {
					e.printStackTrace();
					LogUtil.error("产品下设备列表查询失败！" + JSON.toJSONString(response.getData()));
				}

			}
		}
		return infoList;
	}

	@Override
	public void deleteIotEquipment(String id) {

	}
}
